<!DOCTYPE html>
<html>
<head><meta charset="utf-8" />
<title>104-字典</title>

<script src="/lib/require/index.js"></script>
<script src="/lib/jquery/index.js"></script>

<!-- 改为引入外部样式 -->
<link rel="stylesheet" href="/css/custom.css">

<!-- Loading mathjax macro -->
<!-- Load mathjax -->
    <script src="/lib/mathjax/index.js"></script>
    <!-- MathJax configuration -->
    <script type="text/x-mathjax-config">
    MathJax.Hub.Config({
        tex2jax: {
            inlineMath: [ ['$','$'], ["\\(","\\)"] ],
            displayMath: [ ['$$','$$'], ["\\[","\\]"] ],
            processEscapes: true,
            processEnvironments: true
        },
        // Center justify equations in code and markdown cells. Elsewhere
        // we use CSS to left justify single line equations in code cells.
        displayAlign: 'center',
        "HTML-CSS": {
            styles: {'.MathJax_Display': {"margin": 0}},
            linebreaks: { automatic: true }
        }
    });
    </script>
    <!-- End of mathjax configuration --></head>
<body>
<div class="toc">
<ul>
                        
                        
                        <li class="toc-li-2">
                            <a href="#字典">字典</a>
                        </li>
                    
                
            
            
            
            
            
            
                        
                        
                        <li class="toc-li-3">
                            <a href="#基本操作">基本操作</a>
                        </li>
                    
                
            
            
            
            
            
            
            
                        
                        
                        <li class="toc-li-4">
                            <a href="#len(d)">len(d)</a>
                        </li>
                    
                
            
            
                        
                        
                        <li class="toc-li-4">
                            <a href="#d[key]">d[key]</a>
                        </li>
                    
                
            
            
                        
                        
                        <li class="toc-li-4">
                            <a href="#d[key]-=-value">d[key] = value</a>
                        </li>
                    
                
            
            
                        
                        
                        <li class="toc-li-4">
                            <a href="#del-d[key]">del d[key]</a>
                        </li>
                    
                
            
            
                        
                        
                        <li class="toc-li-4">
                            <a href="#key-in-d">key in d</a>
                        </li>
                    
                
            
            
                        
                        
                        <li class="toc-li-3">
                            <a href="#常用字典方法">常用字典方法</a>
                        </li>
                    
                
            
                        
                        
                        <li class="toc-li-4">
                            <a href="#dict.copy()">dict.copy()</a>
                        </li>
                    
                
            
            
                        
                        
                        <li class="toc-li-4">
                            <a href="#dict.clear()">dict.clear()</a>
                        </li>
                    
                
            
            
            
                        
                        
                        <li class="toc-li-4">
                            <a href="#dict.get(key,-default=None)">dict.get(key, default=None)</a>
                        </li>
                    
                
            
            
            
                        
                        
                        <li class="toc-li-4">
                            <a href="#dict.setdefault(key,-default=None)">dict.setdefault(key, default=None)</a>
                        </li>
                    
                
            
            
                        
                        
                        <li class="toc-li-4">
                            <a href="#items-keys-values">items keys values</a>
                        </li>
                    
                
            
            
            
            
            
            
            
            
                        
                        
                        <li class="toc-li-4">
                            <a href="#dict.pop(key,[,d])">dict.pop(key,[,d])</a>
                        </li>
                    
                
            
            
                        
                        
                        <li class="toc-li-4">
                            <a href="#dict.update(dict2)">dict.update(dict2)</a>
                        </li>
                    
                
            
            </ul>
</div>
  <div tabindex="-1" id="notebook" class="border-box-sizing">
    <div class="container" id="notebook-container">

<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h2 id="&#23383;&#20856;">&#23383;&#20856;<a class="anchor-link" href="#&#23383;&#20856;">&#182;</a></h2><p>字典(dictionary)是另一种<code>可变</code>容器模型，且可存储任意类型对象，用dict()表示。<br>
字典的每个键值{key:value}对用冒号<code>:</code>分割，每个对之间用逗号<code>,</code>分割，整个字典包括在花括号<code>{}</code>中</p>
<blockquote><p>键必须是<code>唯一</code>的，但值不必。值可以取任何数据类型，但键必须是不可变的，如字符串，数字或元组。</p>
</blockquote>
<h3 id="&#22522;&#26412;&#25805;&#20316;">&#22522;&#26412;&#25805;&#20316;<a class="anchor-link" href="#&#22522;&#26412;&#25805;&#20316;">&#182;</a></h3><p>字典对象是以键值对的形式存储数据的，所以只要知道键，就能得到值。这本质上就是一种映射关系。<br>
既然字典是键值对的映射，就不用考虑所谓“排序”问题了，只要通过键就能找到值，至于这个键值对位置在哪里就不用考虑了。没有排序也就不用索引和切片了</p>
<blockquote><p>字典中的这类以键值对的映射方式存储数据，是一种非常高效的方法，比如要读取值得时候，<br>
如果用列表，Python需要从头开始读，直到找到指定的那个索引值。但是，在字典中是通过“键”来得到值。要高效得多。<br>
正是这个特点，键值对这样的形式可以用来存储大规模的数据，因为检索快捷。规模越大越明显。<br>
所以，mongdb这种非关系型数据库在大数据方面比较流行了。</p>
</blockquote>

</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h4 id="len(d)">len(d)<a class="anchor-link" href="#len(d)">&#182;</a></h4><p>返回字典(d)中的键值对的数量</p>

</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[4]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">d</span> <span class="o">=</span> <span class="p">{</span><span class="mi">1</span><span class="p">:</span> <span class="s1">&#39;a&#39;</span><span class="p">,</span> <span class="s1">&#39;b&#39;</span><span class="p">:</span> <span class="mi">2</span><span class="p">}</span>
<span class="nb">len</span><span class="p">(</span><span class="n">d</span><span class="p">)</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area">

<div class="prompt output_prompt">Out[4]:</div>




<div class="output_text output_subarea output_execute_result">
<pre>2</pre>
</div>

</div>

</div>
</div>

</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h4 id="d[key]">d[key]<a class="anchor-link" href="#d[key]">&#182;</a></h4><p>返回字典(d)中的键(key)的值</p>

</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[5]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">d</span> <span class="o">=</span> <span class="p">{</span><span class="mi">1</span><span class="p">:</span> <span class="s1">&#39;a&#39;</span><span class="p">,</span> <span class="s1">&#39;b&#39;</span><span class="p">:</span> <span class="mi">2</span><span class="p">}</span>
<span class="n">d</span><span class="p">[</span><span class="s1">&#39;b&#39;</span><span class="p">]</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area">

<div class="prompt output_prompt">Out[5]:</div>




<div class="output_text output_subarea output_execute_result">
<pre>2</pre>
</div>

</div>

</div>
</div>

</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h4 id="d[key]-=-value">d[key] = value<a class="anchor-link" href="#d[key]-=-value">&#182;</a></h4><p>更新已有key的value或添加一对键值</p>

</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[9]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">d</span> <span class="o">=</span> <span class="p">{</span><span class="mi">1</span><span class="p">:</span><span class="s1">&#39;a&#39;</span><span class="p">,</span><span class="s1">&#39;b&#39;</span><span class="p">:</span><span class="mi">2</span><span class="p">}</span>
<span class="n">d</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">=</span> <span class="s1">&#39;c&#39;</span>
<span class="n">d</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area">

<div class="prompt output_prompt">Out[9]:</div>




<div class="output_text output_subarea output_execute_result">
<pre>{1: &#39;c&#39;, &#39;b&#39;: 2}</pre>
</div>

</div>

</div>
</div>

</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[11]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">d</span><span class="p">[</span><span class="mi">3</span><span class="p">]</span> <span class="o">=</span> <span class="s1">&#39;python&#39;</span>
<span class="n">d</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area">

<div class="prompt output_prompt">Out[11]:</div>




<div class="output_text output_subarea output_execute_result">
<pre>{1: &#39;c&#39;, &#39;b&#39;: 2, 3: &#39;python&#39;}</pre>
</div>

</div>

</div>
</div>

</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h4 id="del-d[key]">del d[key]<a class="anchor-link" href="#del-d[key]">&#182;</a></h4><p>删除字典(d)的键(key)项（将该键值对删除）</p>

</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[13]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">d</span> <span class="o">=</span> <span class="p">{</span><span class="mi">1</span><span class="p">:</span><span class="s1">&#39;a&#39;</span><span class="p">,</span><span class="s1">&#39;b&#39;</span><span class="p">:</span><span class="mi">2</span><span class="p">}</span>
<span class="k">del</span> <span class="n">d</span><span class="p">[</span><span class="s1">&#39;b&#39;</span><span class="p">]</span>
<span class="n">d</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area">

<div class="prompt output_prompt">Out[13]:</div>




<div class="output_text output_subarea output_execute_result">
<pre>{1: &#39;a&#39;}</pre>
</div>

</div>

</div>
</div>

</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h4 id="key-in-d">key in d<a class="anchor-link" href="#key-in-d">&#182;</a></h4><p>检查字典(d)中是否含有键为key的项</p>

</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[17]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">d</span> <span class="o">=</span> <span class="p">{</span><span class="mi">1</span><span class="p">:</span><span class="s1">&#39;a&#39;</span><span class="p">,</span><span class="s1">&#39;b&#39;</span><span class="p">:</span><span class="mi">2</span><span class="p">}</span>
<span class="s1">&#39;b&#39;</span> <span class="ow">in</span> <span class="n">d</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area">

<div class="prompt output_prompt">Out[17]:</div>




<div class="output_text output_subarea output_execute_result">
<pre>True</pre>
</div>

</div>

</div>
</div>

</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[16]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="s1">&#39;c&#39;</span> <span class="ow">in</span> <span class="n">d</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area">

<div class="prompt output_prompt">Out[16]:</div>




<div class="output_text output_subarea output_execute_result">
<pre>False</pre>
</div>

</div>

</div>
</div>

</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h3 id="&#24120;&#29992;&#23383;&#20856;&#26041;&#27861;">&#24120;&#29992;&#23383;&#20856;&#26041;&#27861;<a class="anchor-link" href="#&#24120;&#29992;&#23383;&#20856;&#26041;&#27861;">&#182;</a></h3>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[1]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="p">[</span><span class="n">i</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">dir</span><span class="p">(</span><span class="nb">dict</span><span class="p">())</span> <span class="k">if</span> <span class="ow">not</span> <span class="n">i</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s1">&#39;_&#39;</span><span class="p">)]</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area">

<div class="prompt output_prompt">Out[1]:</div>




<div class="output_text output_subarea output_execute_result">
<pre>[&#39;clear&#39;,
 &#39;copy&#39;,
 &#39;fromkeys&#39;,
 &#39;get&#39;,
 &#39;items&#39;,
 &#39;keys&#39;,
 &#39;pop&#39;,
 &#39;popitem&#39;,
 &#39;setdefault&#39;,
 &#39;update&#39;,
 &#39;values&#39;]</pre>
</div>

</div>

</div>
</div>

</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h4 id="dict.copy()">dict.copy()<a class="anchor-link" href="#dict.copy()">&#182;</a></h4><p>函数返回一个字典的浅复制</p>

</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h4 id="dict.clear()">dict.clear()<a class="anchor-link" href="#dict.clear()">&#182;</a></h4><p>将字典<code>清空</code>(del是删除)，修改原对象，<code>没有</code>返回值</p>
<h4 id="dict.get(key,-default=None)">dict.get(key, default=None)<a class="anchor-link" href="#dict.get(key,-default=None)">&#182;</a></h4><p>返回指定键的值，如果值不在字典则<code>返回</code>默认值</p>
<h4 id="dict.setdefault(key,-default=None)">dict.setdefault(key, default=None)<a class="anchor-link" href="#dict.setdefault(key,-default=None)">&#182;</a></h4><p><code>返回</code>指定键的值，如果值不在字典中则添加这对(key:default)<code>没有</code>返回值</p>

</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h4 id="items-keys-values">items keys values<a class="anchor-link" href="#items-keys-values">&#182;</a></h4>
<pre><code>dict.items()    # 以列表的`形式`返回可遍历的(键, 值) 元组数组
dict.keys()     # 以列表的`形式`返回字典所有的键
dict.values()   # 以列表的`形式`返回字典所有的值</code></pre>
<p>返回的并不是list, 而是<a href="https://docs.python.org/3/library/stdtypes.html#dict-views">view对象</a></p>

</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h4 id="dict.pop(key,[,d])">dict.pop(key,[,d])<a class="anchor-link" href="#dict.pop(key,[,d])">&#182;</a></h4><p>删除指定键的键值对，如果key不在字典中则返回d，如果key不在字典中又没有d，则报错</p>

</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h4 id="dict.update(dict2)">dict.update(dict2)<a class="anchor-link" href="#dict.update(dict2)">&#182;</a></h4><p>更新原字典，<code>没有</code>返回值</p>

</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[18]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">x</span> <span class="o">=</span> <span class="p">{</span><span class="s1">&#39;a&#39;</span><span class="p">:</span><span class="mi">1</span><span class="p">}</span>
<span class="n">y</span> <span class="o">=</span> <span class="p">{</span><span class="s1">&#39;b&#39;</span><span class="p">:</span><span class="mi">2</span><span class="p">}</span>
<span class="n">x</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="n">y</span><span class="p">)</span>
<span class="n">x</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area">

<div class="prompt output_prompt">Out[18]:</div>




<div class="output_text output_subarea output_execute_result">
<pre>{&#39;a&#39;: 1, &#39;b&#39;: 2}</pre>
</div>

</div>

</div>
</div>

</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[22]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">z</span> <span class="o">=</span> <span class="p">[(</span><span class="s1">&#39;c&#39;</span><span class="p">,</span><span class="mi">3</span><span class="p">)]</span>  <span class="c1"># 类似dict.items</span>
<span class="n">x</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="n">z</span><span class="p">)</span>
<span class="n">x</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area">

<div class="prompt output_prompt">Out[22]:</div>




<div class="output_text output_subarea output_execute_result">
<pre>{&#39;a&#39;: 1, &#39;b&#39;: 2, &#39;c&#39;: 3}</pre>
</div>

</div>

</div>
</div>

</div>
    </div>
  </div>
</body>

 


</html>
